let bannerBox = document.getElementById('banner-box');
let goTop = document.getElementById('goTop');
let allShop = document.getElementById('allshop');
let bannerContainer = document.getElementById('banner-container');
let now = 0;
let timer = null;
let btnCl = document.getElementById('reg')
let ntime = document.querySelector('.time strong')
let hour = document.querySelector('.time .hour')
let min = document.querySelector('.time .minute')
let sec = document.querySelector('.time .second')
let isul = document.querySelector('.isul')
let mycontent = document.querySelector('.mycontent')
let logo = document.querySelector('.logo')
let addgood = document.querySelector('.btn-brank-one')
let buyGood = document.querySelector('.btn-brank-two')
let btnBox = document.querySelectorAll('.btn-box')
let spanShow = document.querySelector('.span-icon')
spanShow.onmouseover = function () {
    spanShow.nextElementSibling.style.display = 'block'
}
spanShow.nextElementSibling.onmouseleave = function () {
    spanShow.nextElementSibling.style.display = "none"
}
logo.onclick = function () {
    window.location.href = 'http://127.0.0.1:5500/xiaomiyoupin/index.html'
}


function changeColor(tag) {
    tag.style.color = '#fff'
    tag.style.backgroundColor = "rgba(132,95,63)"
}

let speed = 180;
let speedTimer = setInterval(function () {
    speed -= 5;
    if (speed <= 0) {
        speed = 180;
    }
}, 500);

goTop.onclick = function () {
    let scrollY = window.scrollY;
    let timer = setInterval(function () {
        scrollY -= speed;
        if (scrollY <= 0) {
            clearInterval(timer);
        }
        window.scrollTo(0, scrollY);
    }, 30);
}
let data = localStorage.getItem('now')
if (data) {
    data = JSON.parse(data)
    Array.from(data).forEach((v) => {

        document.querySelector('.top1').innerHTML = v.tips
        document.querySelector('.money-value').innerHTML = v.price
        document.querySelector('.w2w').innerHTML=v.id
        document.querySelector('.img img').src=v.img1
        document.querySelector('.thumb-img .img-one img').src=v.img1
        document.querySelector('.thumb-img .img-two img').src=v.img2
        document.querySelector('.thumb-img .img-three img').src=v.img3
        document.querySelector('.thumb-img .img-four img').src=v.img4
    })

}
let imgww=document.querySelector('.thumb-img')
    imgww.onclick=function(e){
        document.querySelector('.img img').src =e.target.src
        Array.from(imgww.children).forEach((v)=>{
            if(v.classList.contains('img-chicked')){
                v.classList.remove('img-chicked')
            }
        })
        e.target.parentNode.classList.add('img-chicked')
        
    }

let names = localStorage.getItem('已登录')
if (names) {
    let html = ''
    html = `
        <div type="button" style="color:#E7E7E7 ;position: relative;" id="reg">
            <img src="https://img.youpin.mi-img.com/shopcenter/7dhur6i77hg_11350267611625208274363.png" width=30
                alt="">
            <div>我&nbsp;<svg fill="#ccc" xmlns="http://www.w3.org/2000/svg" style="width: 12px;height:7px"
                    viewBox="0 0 40 22.5">
                    <path
                        d="M39.28.72h0a2.49,2.49,0,0,0-3.5,0L20,16.53,4.23.72A2.48,2.48,0,0,0,.72,4.23L18.24,21.78A2.48,2.48,0,0,0,20,22.5a2.52,2.52,0,0,0,1.77-.72L39.28,4.23A2.5,2.5,0,0,0,39.28.72Z">
                    </path>
                </svg>
            </div>
            <ul class="isul">
                <li class="isli">我的订单</li>
                <li>退款/售后</li>
                <li>我的资产</li>
                <li>我的收藏</li>
                <li>地址管理</li>
                <li>退出登录</li>
            </ul>
        </div>`
    document.querySelector("#mycontent").innerHTML = html
    let isul = document.querySelector('.isul')
    let mycontent = document.querySelector('.mycontent')
    mycontent.onmouseover = function () {
        isul.style.display = 'block'
    }
    isul.onmouseout = function (e) {
        isul.style.display = 'none'

    }
    let liObj = isul.children
    Array.from(liObj).forEach((v) => {
        if (v.innerHTML == '退出登录') {
            v.onclick = function () {
                console.log(1);
                localStorage.removeItem('已登录')
                location.reload()
            }
        }
        if (v.innerHTML == '我的订单') {
            v.onclick = function () {
                location.href = 'http://127.0.0.1:5500/xiaomiyoupin/user.html'
            }
        }
        if (v.innerHTML == '我的收藏') {
            v.onclick = function () {
                location.href = 'http://127.0.0.1:5500/xiaomiyoupin/collect.html'

            }
        }
        if (v.innerHTML == '我的资产') {
            v.onclick = function () {
                location.href = 'http://127.0.0.1:5500/xiaomiyoupin/asset.html'
            }
        }
        if (v.innerHTML == '地址管理') {
            v.onclick = function () {
                location.href = 'http://127.0.0.1:5500/xiaomiyoupin/address.html'
            }
        }
    })
} else {
    let btnCl = document.getElementById('reg')
    btnCl.onclick = function btnClFn() {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true,
            skin: '',
            btn: ['同意并继续', '不同意'],
            content: `<div class="undefined layout-horizontal-vertical"><span class="close layui-layer-close"><div style="width: 13px; height: 13px; cursor: default;"><svg fill="#ccc" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path d="M32.5.68,18,15.17,3.5.68A2,2,0,0,0,.68,3.5L15.17,18,.68,32.5A2,2,0,0,0,3.5,35.32L18,20.83,32.5,35.32a2,2,0,0,0,2.82-2.82L20.83,18,35.32,3.5A2,2,0,0,0,32.5.68Z"></path></svg></div></span><div class="agreement-container"><p class="title">声明与政策</p><div class="content"><p>欢迎您来到小米有品！</p><p>我们依据最新法律法规要求，制定并更新了<a target="_blank" href="https://daren.xiaomiyoupin.com/ewen/pageFromId?id=cxsew05x0x0ch53v" rel="noopener noreferrer">《隐私政策》</a><span class="color-gold">、</span><a target="_blank" href="https://m.xiaomiyoupin.com/mr/60013c77c9e77c0001f35060" rel="noopener noreferrer">《小米有品用户协议》</a>以及<a target="_blank" href="https://m.xiaomiyoupin.com/mr/60014809cff47e0001f66ad6" rel="noopener noreferrer">《小米帐号使用协议》</a>。</p><p>您需阅读并同意相关政策条款方可进行登录。</p></div></div>`
        });
        document.querySelector('.layui-layer-btn0').href = 'http://127.0.0.1:5500/xiaomiyoupin/login.html'
    }
}

let cart = document.querySelector('.cart-icon')
cart.onclick = function () {
    window.location.href = 'http://127.0.0.1:5500/xiaomiyoupin/cart.html'
}
document.querySelector('#license').onclick = function () {
    window.location.href = "https://daren.xiaomiyoupin.com/ewen/pageFromId?id=gurkg3d4bh7bbe6q"
}
document.querySelector('#protocol').onclick = function () {
    window.location.href = "https://daren.xiaomiyoupin.com/ewen/pageFromId?id=ytf4mzipem9cr7fj"
}
let addObj = document.querySelector('.count .add')
let valueObj = 1
addObj.previousElementSibling.onblur = function () {
    valueObj = addObj.previousElementSibling.value - 0

}
addObj.onclick = function () {
    valueObj = valueObj + 1
    addObj.previousElementSibling.value = valueObj
}
addObj.previousElementSibling.previousElementSibling.onclick = function () {
    if (valueObj == 1) {
        addObj.previousElementSibling.previousElementSibling.style.userSelect = 'none'
    } else {
        valueObj = valueObj - 1
        addObj.previousElementSibling.value = valueObj

    }
}

document.querySelector('.btn-brank-one').onclick = function () {
    layer.msg('添加成功', {
        color: '#51a351',
        offset: "t",
    });
    let data = localStorage.getItem('now')
    if (data) {
        data = JSON.parse(data)
        Array.from(data).forEach((v) => {
            let id = v.id
            let src = v.src
            let price = v.price
            let tips = v.tips
            let num = valueObj
            document.querySelector('.img img').src = v.src
            document.querySelector('.top1').innerHTML = v.tips
            document.querySelector('.money-value').innerHTML = v.price
            
            let cartGoods = localStorage.getItem('cart')
            if (cartGoods) {
                let offOn = false
                cartGoods = JSON.parse(cartGoods)
                cartGoods.forEach((v) => {
                    if (v.id == id) {
                        v.num = v.num - 0 + num - 0
                        offOn = true
                    }
                })
                if (!offOn) {
                    cartGoods.push({
                        id,
                        src,
                        price,
                        tips,
                        num
                    })
                }
                localStorage.setItem('cart', JSON.stringify(cartGoods))
            } else {
                let cartAdd = {
                    id,
                    src,
                    price,
                    tips,
                    num
                }
                cartArr = [cartAdd]
                localStorage.setItem('cart', JSON.stringify(cartArr))
            }
        })

    }
    setTimeout(() => {
        window.location.reload()
    }, 3000);

}
let id = document.querySelector('.w2w').innerHTML
let src = document.querySelector('.img img').src
let tips = document.querySelector('.top1').innerHTML
let price=document.querySelector('.money-value').innerHTML
let collect = document.querySelector('.collect')
let dataM = localStorage.getItem('collect')
let img1=document.querySelectorAll('.thumb-img img')[0].src
let img2=document.querySelectorAll('.thumb-img img')[1].src
let img3=document.querySelectorAll('.thumb-img img')[2].src
let img4=document.querySelectorAll('.thumb-img img')[3].src
if (dataM) {

    dataM = JSON.parse(dataM)
    Array.from(dataM).forEach((v) => {
        if (v.id == id) {
            collect.children[0].classList.remove('icon-shoucang')
            collect.children[0].classList.add('icon-shoucang1')
            collect.children[0].style.color = 'rgba(132,95,63)'
        }
    })
}

collect.onclick = function () {
    let data = localStorage.getItem(`collect`)
  
    if (data && data !== '[]') {
        let offO = false
        data = JSON.parse(data)
        Array.from(data).forEach((v, i) => {
            if (v.id == id) {
                collect.children[0].classList.remove('icon-shoucang1')
                collect.children[0].classList.add('icon-shoucang')
                collect.children[0].style.color = '#999'
                data.splice(i, 1)
                offO = true
                layer.msg('取消收藏', {
                    color: '#51a351',
                    offset: "t",
                });
            }
        })
        if(!offO){
            collect.children[0].classList.remove('icon-shoucang')
            collect.children[0].classList.add('icon-shoucang1')
            collect.children[0].style.color = 'rgba(132,95,63)'
            data.push({
                id,
                src,
                tips,
                price,
                img1,
                img2,
                img3,
                img4
            })
            layer.msg('收藏成功', {
                color: '#51a351',
                offset: "t",
            });
         
        }
        localStorage.setItem('collect', JSON.stringify(data))

    } else {
        collect.children[0].classList.remove('icon-shoucang')
        collect.children[0].classList.add('icon-shoucang1')
        collect.children[0].style.color = 'rgba(132,95,63)'
        let datas = {
            id: id,
            src: src,
            tips,
            price,
            img1,
            img2,
            img3,
            img4
        }
        console.log(id);
        datas = [datas]
        localStorage.setItem('collect', JSON.stringify(datas))
        offO = true
        layer.msg('收藏成功', {
            color: '#51a351',
            offset: "t",
        });
    }
   
}
document.querySelector('.btn-brank-two').onclick=function(){
    let num=document.querySelector('#goods-count').value
    localStorage.setItem('buyNum',num)
    location.href='http://127.0.0.1:5500/xiaomiyoupin/buy.html'
}